//
// Created by Administrator on 2023/8/4.
//
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<int> answerQueries(vector<int> &nums, vector<int> &queries) {
        sort(nums.begin(), nums.end());
        // nums的前缀和
        vector<int> preSum(nums.size(), 0);
        for (int i = 0; i < nums.size(); i++) {
            preSum[i] = nums[i] + (i - 1 >= 0 ? preSum[i - 1] : 0);
        }
        // 查询
        vector<int> ans;
        for (auto &tat: queries) {
            ans.push_back(upper_bound(preSum.begin(), preSum.end(), tat) - preSum.begin()) ;
        }
        return ans;
    }
};

int main() {
    vector<int> nums = {4, 5, 2, 1};
    vector<int> queries = {3, 10, 21};
    Solution solution;
    vector<int> answerQueries = solution.answerQueries(nums, queries);
    for (int answerQuerie: answerQueries) {
        cout << answerQuerie << endl;
    }
    return 0;
}